﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;

using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

namespace Ko
{
    public class SPUsersHelper
    {

        public Exception GetUsers(ref DataTable tbl, string sURL)
        {
            Exception ret = null;

            ret = MakeTableForUsersList(ref tbl);

            if (ret != null)
                return ret;

            try
            {
                using (SPSite site = new SPSite(sURL))
                {
                    using (SPWeb spUWeb = site.OpenWeb())
                    {

                        string OriginalUrl = site.Url;
                        SPServiceContext context = SPServiceContext.GetContext(site);
                        UserProfileManager uprofileManager = new UserProfileManager(context);

                        #region foreach

                        foreach (UserProfile uPr in uprofileManager)
                        {

                            DataRow r = tbl.NewRow();

                            if (uPr["UserProfile_GUID"].Value != null)
                                r["ID"] = uPr["UserProfile_GUID"].Value.ToString();
                            if (uPr["ID1C"].Value != null)
                                r["ID1C"] = uPr["ID1C"].Value.ToString();
                            if (uPr["ADGuid"].Value != null)
                                r["ADGuid"] = uPr["ADGuid"].Value.ToString();
                            if (uPr["AccountName"].Value != null)
                                r["AccountName"] = uPr["AccountName"].Value.ToString();
                            if (uPr["SPS-DistinguishedName"].Value != null)
                                r["DistinguishedName"] = uPr["SPS-DistinguishedName"].Value.ToString();
                            if (uPr["SPS-SavedAccountName"].Value != null)
                                r["SavedAccountName"] = uPr["SPS-SavedAccountName"].Value.ToString();
                            if (uPr["SPS-SourceObjectDN"].Value != null)
                                r["SourceObjectDN"] = uPr["SPS-SourceObjectDN"].Value.ToString();
                            if (uPr["PreferredName"].Value != null)
                                r["FullName"] = uPr["PreferredName"].Value.ToString();
                            if (uPr["FirstName"].Value != null)
                                r["FirstName"] = uPr["FirstName"].Value.ToString();
                            if (uPr["LastName"].Value != null)
                                r["LastName"] = uPr["LastName"].Value.ToString();
                            if (uPr["SPS-JobTitle"].Value != null)
                                r["Staff"] = uPr["SPS-JobTitle"].Value.ToString();
                            if (uPr["Department"].Value != null)
                                r["Department"] = uPr["Department"].Value.ToString();
                            if (uPr["Office"].Value != null)
                                r["Office"] = uPr["Office"].Value.ToString();
                            if (uPr["WorkPhone"].Value != null)
                                r["WorkPhone"] = uPr["WorkPhone"].Value.ToString();
                            if (uPr["PictureUrl"].Value != null)
                                r["ImageUrl"] = uPr["PictureUrl"].Value.ToString();
                            else
                                r["ImageUrl"] = sURL + "/_layouts/IMAGES/no-photo.png";
                            if (uPr.PersonalSite != null)
                                r["SiteUrl"] = uPr.PersonalSite.Url.ToString();

                            DateTime dtBirthday, dtHireDate;
                            if (uPr["SPS-Birthday"].Value != null)
                            {
                                dtBirthday = DateTime.Parse(uPr["SPS-Birthday"].Value.ToString());
                                r["Birthday"] = dtBirthday;
                            }
                            if (uPr["SPS-HireDate"].Value != null)
                            {
                                dtHireDate = DateTime.Parse(uPr["SPS-HireDate"].Value.ToString());
                                r["HireDate"] = dtHireDate;
                            }

                            tbl.Rows.Add(r);
                        }

                        #endregion

                    }
                }
            }
            catch (Exception err)
            {
                ret = new Exception(err.Message, err);
            }

            return ret;
        }

        public Exception MakeTableForUsersList(ref DataTable tbl)
        {
            Exception ret = null;
            tbl = new DataTable("Users");
            tbl.Columns.Add("ID", typeof(Guid));
            tbl.Columns.Add("ID1C", typeof(string));
            tbl.Columns.Add("ADGuid", typeof(string));
            tbl.Columns.Add("AccountName", typeof(string));
            tbl.Columns.Add("DistinguishedName", typeof(string));
            tbl.Columns.Add("SavedAccountName", typeof(string));
            tbl.Columns.Add("SourceObjectDN", typeof(string));
            tbl.Columns.Add("FirstName", typeof(string));
            tbl.Columns.Add("LastName", typeof(string));
            tbl.Columns.Add("FullName", typeof(string));
            tbl.Columns.Add("Department", typeof(string));
            tbl.Columns.Add("Office", typeof(string));
            tbl.Columns.Add("ImageUrl", typeof(string));
            tbl.Columns.Add("SiteUrl", typeof(string));
            tbl.Columns.Add("WorkPhone", typeof(string));
            tbl.Columns.Add("Mail", typeof(string));
            tbl.Columns.Add("Staff", typeof(string));
            tbl.Columns.Add("Birthday", typeof(DateTime));
            tbl.Columns.Add("HireDate", typeof(DateTime));

            return ret;
        }

    }
}


